package com.cwd.arm.common.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Cell;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * Created by yan on 22-2-4.
 */
@Slf4j
public class StringUtil {

    /**
     * 转码
     *
     * @param str        　被转码字符
     * @param formatFrom 　本来的编码
     * @param FormatTo   转换后的编码
     * @return
     */
    public static String convertEncodingFormat(String str, String formatFrom, String FormatTo) {
        String result = null;
        if (!(str == null || str.length() == 0)) {
            try {
                result = new String(str.getBytes(formatFrom), FormatTo);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                log.error("StringUtil.convertEncodingFormat:{}", e.getMessage(), e);
            }
        }
        return result;
    }

    public static String getEncoding(String str) {
        String encode;

        encode = "UTF-16";
        try {
            if (str.equals(new String(str.getBytes(), encode))) {
                return encode;
            }
        } catch (Exception ex) {
        }

        encode = "ASCII";
        try {
            if (str.equals(new String(str.getBytes(), encode))) {
                return "字符串<< " + str + " >>中仅由数字和英文字母组成，无法识别其编码格式";
            }
        } catch (Exception ex) {
        }

        encode = "ISO-8859-1";
        try {
            if (str.equals(new String(str.getBytes(), encode))) {
                return encode;
            }
        } catch (Exception ex) {
        }

        encode = "GB2312";
        try {
            if (str.equals(new String(str.getBytes(), encode))) {
                return encode;
            }
        } catch (Exception ex) {
        }

        encode = "UTF-8";
        try {
            if (str.equals(new String(str.getBytes(), encode))) {
                return encode;
            }
        } catch (Exception ex) {
        }

        /*
     *......待完善
	 */

        return "未识别编码格式";
    }

    public static String listToString(List<String> stringList) {
        String s = "";
        for (int i = 0; i < stringList.size(); i++) {
            if (i == 0) {
                s += stringList.get(i);
            } else {
                s += "," + stringList.get(i);
            }
        }
        return s;
    }

    public static boolean isNumeric(String str) {
        String[] ss = str.split("\\.");
        for (String s : ss) {
            if (s.contains("E")) {
                String[] es = s.split("E");
                if (es[1].length() != 1) {
                    return false;
                }
                if (notNum(es[0])) {
                    return false;
                }
            }else{
                if (notNum(s)) {
                    return false;
                }
            }
        }
        return true;
    }

    public static String toNorNumeric(String e){
        if(e.contains("E")){
            e = e.replace(".","").replace("E","");
        }
        return e;
    }

    private static boolean notNum(String s) {
        for (int i = s.length(); --i >= 0; ) {
            if (!Character.isDigit(s.charAt(i))) {
                return true;
            }
        }
        return false;
    }

}
